package edu.lingnan.rili.controller;


import com.alibaba.excel.EasyExcel;
import edu.lingnan.rili.common.DataListener;
import edu.lingnan.rili.pojo.Administrator;
import edu.lingnan.rili.pojo.Match;
import edu.lingnan.rili.pojo.User;
import edu.lingnan.rili.service.AdministratorService;
import edu.lingnan.rili.service.MatchService;
import edu.lingnan.rili.utils.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/administrator")
@Api(tags = "管理员控制器")
@Slf4j
public class AdministratorController {
    @Autowired
    private AdministratorService administratorService;

    @Autowired
    private MatchService matchService;

    @ApiOperation("管理员登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "管理员登录账号"),
            @ApiImplicitParam(name = "password", value = "管理员登录密码")
    })
    @PostMapping("/login")
    public Result<Object> login(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        @ApiIgnore HttpSession session) {
        if (StringUtils.isAnyBlank(account, password)) {
            return new Result<>(201, "请输入账号密码");
        }
        Administrator administrator = administratorService.getById(Integer.parseInt(account));
        if (ObjectUtils.isNotEmpty(administrator)) {
            Administrator login = administratorService.login(Integer.parseInt(account), password);
            if (ObjectUtils.isNotEmpty(login)) {
                session.setAttribute("admin", login);
                return new Result<>(200, "管理员" + administrator.getAdministratorName() + "登录成功");
            }
            return new Result<>(201, "登录失败，密码错误");
        }
        return new Result<>(201, "登录失败，该用户还未注册");
    }

    /**
     * @description 实现数据批量导入
     * @author xBaozi
     * @date 19:27 2022/5/3
     * @param file  需要导入的文件
     **/
    @ApiOperation("批量导入学生信息")
    @PostMapping("/upload")
    public Result<String>  upload(@ApiParam("学生信息导入文件") @RequestParam("files") MultipartFile file) throws IOException {
        // 获得上传文件的名称
        String filename = file.getOriginalFilename();
        // 获取源文件的后缀名
        int index = filename != null ? filename.lastIndexOf(".") : -1;
        // 对没有后缀名的文件进行判断
        if (index < 0) {
            return new Result<>(201, "请选择正确的文件");
        }
        // 截取.点后面的内容，即后缀名
        String suffix = filename.substring(index + 1);
        log.info("获取后缀名为：{}", suffix);
        //判断是否为excel文档
        if (!"xlsx".equals(suffix) && !"xls".equals(suffix)) {
            return new Result<>(201, "只支持Excel文档导入");
        }
        try {
            EasyExcel.read(file.getInputStream(), Match.class, new DataListener<Match>(matchService)).sheet().doRead();
        } catch (Exception e) {
            return new Result<>(201, "导入失败，请重试");
        }
        return new Result<>(200, "批量导入成功");
    }

    /**
     * @description 判断管理员是否登录或登录过期
     * @author xBaozi
     * @date 9:01 2022/4/8
     * @return      如果登陆超时或未登录为true，否则false
     **/
    public static boolean isAdminLogin(HttpSession session) {
        // 判断是否登录或者登录过期
        Administrator admin = (Administrator) session.getAttribute("admin");
        return admin == null;
    }
}

